package com.offcn.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author: zola
 * @Date: 2021/9/16 - 09 - 16 - 20:31
 * @Description: com.offcn.gateway.config
 * @Version: 1.0
 */
@Configuration
public class TokenConfig {
    /**
     * @return: KeyResolver
     * @description: 使用ip来限流
     * @analyze: Mono<T> just(T data)
     */
    @Bean(name = "ipResolver")
    public KeyResolver userKeyResolver(){
        return new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                String hostIP = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress();
                System.out.println(hostIP);
                return Mono.just(hostIP);
            }
        };
    }
}
